<template>
  <div class="container">
    <div style="width: 400px; padding: 30px; background-color: white; border-radius: 5px;">
      <div style="text-align: center; font-size: 20px; margin-bottom: 20px; color: #333">欢迎登录健康管理系统</div>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" placeholder="请输入账号" v-model="form.userName"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" show-password  v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item prop="role" label="">
          <el-select v-model="form.userRole" clearable placeholder="请选择">
            <el-option
                v-for="item in loginRoles"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%; background-color: #7eb488; border-color: #7eb488; color: white" @click="doSubmit('login')">登 录</el-button>
        </el-form-item>
        <div style="display: flex; align-items: center">
          <div style="flex: 1"></div>
          <div style="flex: 1; text-align: right">还没有账号？请 <a href="/register">注册</a>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import {roles} from "@/utils/select";
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "login.vue" ,
  data () {
    return {
      form:{
        userName:'',
        password:'',
        userNumber:'',
        userRole:'',
      },
      loginRoles: roles,
      loginLoading: false
    }
  },
  created () {},
  methods: {
    doSubmit (type) {
        if (type==='login'){
          if (this.form.userName === '' || this.form.password=== ''){
            this.$message.error('用户名或者密码不得存在空值！！！')
            return
          }
          let submitData = {
            userName:this.form.userName,
            role:this.form.userRole,
            passWord:this.form.password
          }
          axios.post('http://localhost:4009/login', submitData).then(res => {
            console.log(res)
            if (res.data.status !== 200) {
              this.$message.error(res.data.message)
              return;
            } else {
              this.$message({
                message: '登录成功！！',
                type: 'success'
              })
              if (this.form.userRole.includes('管理员')){
                // 开始跳转到Manager 后台管理页面
                console.log("开始跳转到Manager 后台管理页面 "+this.form.userRole)
                this.$router.push(`/admin/${this.form.userRole}/${this.form.userName}`)
              }else {
                // 开始跳转到 普通用户发帖页面
                console.log("开始跳转到 普通用户页面")
                this.$router.push(`/user/${this.form.userName}`)
              }
            }
            this.form.userName=''
            this.form.password=''
            this.form.userRole=''
          }).catch(error => {
            console.log(error)
            this.$message.error(error)
          })
          // eslint-disable-next-line no-empty
        } else if (type==='register') {
          this.$router.push(`/register`)


        }
    }

  }
}
</script>
<style scoped>
.container {
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/img/bg.jpg");
  background-size: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
}
a {
  color: #2a60c9;
}
</style>
